

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

bool isSameTree(TreeNode* p, TreeNode* q) {
    if(p == nullptr){
        if(q == nullptr) return true;
        return false;
    }
    if(q == nullptr) return false;
    if(p->val != q->val) return 0;
    if(!isSameTree(p->left, q->left)) return 0;
    if(!isSameTree(p->right, q->right)) return 0;
    return 1;
}